package com.practices2.adapter;

import java.util.ArrayList;
import java.util.HashMap;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.Toast;

import com.practices2.R;
import com.practices2.bean.ViewHolderDownlaodList;
import com.practices2.util.download.DownLoadCallback;
import com.practices2.util.download.DownloadManager;

public class DownloadListAdapter extends BaseAdapter {

	private Context mContext;
	private ArrayList<HashMap<Integer, String>> datalist;
	private DownloadManager downloadManager;
	private ListView downListView;

	public DownloadListAdapter(Context context, ListView listview) {
		super();
		this.mContext = context;
		this.downListView = listview;

		datalist = new ArrayList<HashMap<Integer, String>>();
		downloadManager = DownloadManager.getDownloadManager();
		downloadManager.setDownLoadCallback(new DownLoadCallback() {

			@Override
			public void onLoading(String url, long totalSize, long currentSize, long speed) {
				super.onLoading(url, totalSize, currentSize, speed);

				long downPercent = currentSize * 100 / totalSize;

				View tastListItem = downListView.findViewWithTag(url);
				ViewHolderDownlaodList viewHolder = new ViewHolderDownlaodList(tastListItem);

				viewHolder.setData(url, "speed" + speed + "kps" + "||" + currentSize + "||" + totalSize, downPercent + "");

			}

			@Override
			public void onSuccess(String url) {
				super.onSuccess(url);
				Toast.makeText(mContext, "downlaod finish", Toast.LENGTH_SHORT).show();
			}

			@Override
			public void onFinish(String url) {
				super.onFinish(url);
			}
		});
	}

	@Override
	public int getCount() {
		return datalist.size();
	}

	@Override
	public Object getItem(int position) {
		return datalist.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = inflater.inflate(R.layout.adpter_item_downloadlist, null);
			// convertView=
			// LayoutInflater.from(mContext).inflate(R.layout.adpter_item_downloadlist,
			// null);
		}

		// convertView 中绑定url 数据
		HashMap<Integer, String> map = datalist.get(position);
		String url = map.get(ViewHolderDownlaodList.KEY_URL);
		convertView.setTag(url);

		ViewHolderDownlaodList viewHolder = new ViewHolderDownlaodList(convertView);
		viewHolder.setData(map);

		viewHolder.btn_continue.setOnClickListener(new MybtnOnClickListener(viewHolder, url));
		viewHolder.btn_delete.setOnClickListener(new MybtnOnClickListener(viewHolder, url));
		viewHolder.btn_pause.setOnClickListener(new MybtnOnClickListener(viewHolder, url));

		return convertView;
	}

	private class MybtnOnClickListener implements View.OnClickListener {

		ViewHolderDownlaodList viewHolder;
		String url;

		public MybtnOnClickListener(ViewHolderDownlaodList viewHolder, String url) {
			super();
			this.viewHolder = viewHolder;
			this.url = url;
		}

		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.btn_continue:

				downloadManager.continueHandler(url);
				viewHolder.btn_continue.setVisibility(View.GONE);
				viewHolder.btn_pause.setVisibility(View.VISIBLE);

				break;
			case R.id.btn_delete:

				downloadManager.deleteHandler(url);

				break;
			case R.id.btn_pause:

				downloadManager.pauseHandler(url);
				viewHolder.btn_continue.setVisibility(View.VISIBLE);
				viewHolder.btn_pause.setVisibility(View.GONE);
				break;

			default:
				break;
			}

		}

	}

	public void addItem(String url) {
		addItem(url, false);
	}

	public void addItem(String url, boolean isPause) {
		HashMap<Integer, String> map = new HashMap<Integer, String>();
		map.put(ViewHolderDownlaodList.KEY_URL, url);
		datalist.add(map);
		this.notifyDataSetChanged();
	}

	public void removeItem(String url) {
		String temp;
		for (int i = 0; i < datalist.size(); i++) {
			temp = datalist.get(i).get(ViewHolderDownlaodList.KEY_URL);
			if (temp.equals(url)) {
				datalist.remove(i);
				this.notifyDataSetChanged();
			}
		}
	}
}
